#pragma once

namespace DABG {

	public ref class frmTrader : public System::Windows::Forms::Form
	{
	public:
		frmTrader(void)
		{
			InitializeComponent();
		}

	protected:
		~frmTrader()
		{
			if (components)
			{
				delete components;
			}
		}
	private: AxTWSLib::AxTws^  Tws1;
	private: System::Windows::Forms::Button^  btnBuy;
	private: System::Windows::Forms::TextBox^  txtStop;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::TextBox^  txtTrigger;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  txtSymbol;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Button^  btnSell;
	public: System::Windows::Forms::ListBox^  lstErr;
	private: System::Windows::Forms::Button^  btnOptions;
	private: System::Windows::Forms::Label^  label10;
	private: System::Windows::Forms::TextBox^  txtSize;
	private: System::Windows::Forms::Button^  btnLimit;
	private: System::Windows::Forms::Button^  btnCloseAll;

	private: System::Windows::Forms::Button^  btnClearLog;
	private: System::Windows::Forms::TabControl^  tbStrategies;
	private: System::Windows::Forms::TabPage^  tb1;


	private: System::Windows::Forms::ListBox^  lstActiveTriggers;

	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::Button^  btnCopy;
	private: System::Windows::Forms::CheckBox^  ckOnTop;
	private: System::Windows::Forms::TabPage^  tb2;

	private: System::ComponentModel::IContainer^  components;

	protected: 

	private:


#pragma region Windows Form Designer generated code
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(frmTrader::typeid));
			this->Tws1 = (gcnew AxTWSLib::AxTws());
			this->btnBuy = (gcnew System::Windows::Forms::Button());
			this->txtStop = (gcnew System::Windows::Forms::TextBox());
			this->btnSell = (gcnew System::Windows::Forms::Button());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->txtTrigger = (gcnew System::Windows::Forms::TextBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->txtSymbol = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->lstErr = (gcnew System::Windows::Forms::ListBox());
			this->btnOptions = (gcnew System::Windows::Forms::Button());
			this->label10 = (gcnew System::Windows::Forms::Label());
			this->txtSize = (gcnew System::Windows::Forms::TextBox());
			this->btnLimit = (gcnew System::Windows::Forms::Button());
			this->btnCloseAll = (gcnew System::Windows::Forms::Button());
			this->btnClearLog = (gcnew System::Windows::Forms::Button());
			this->tbStrategies = (gcnew System::Windows::Forms::TabControl());
			this->tb1 = (gcnew System::Windows::Forms::TabPage());
			this->tb2 = (gcnew System::Windows::Forms::TabPage());
			this->btnCopy = (gcnew System::Windows::Forms::Button());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->lstActiveTriggers = (gcnew System::Windows::Forms::ListBox());
			this->ckOnTop = (gcnew System::Windows::Forms::CheckBox());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->Tws1))->BeginInit();
			this->tbStrategies->SuspendLayout();
			this->tb1->SuspendLayout();
			this->tb2->SuspendLayout();
			this->SuspendLayout();
			// 
			// Tws1
			// 
			this->Tws1->Enabled = true;
			this->Tws1->Location = System::Drawing::Point(1248, 766);
			this->Tws1->Name = L"Tws1";
			this->Tws1->OcxState = (cli::safe_cast<System::Windows::Forms::AxHost::State^  >(resources->GetObject(L"Tws1.OcxState")));
			this->Tws1->Size = System::Drawing::Size(134, 17);
			this->Tws1->TabIndex = 7;
			this->Tws1->TabStop = false;
			this->Tws1->tickPrice += gcnew AxTWSLib::_DTwsEvents_tickPriceEventHandler(this, &frmTrader::Tws1_tickPrice);
			this->Tws1->errMsg += gcnew AxTWSLib::_DTwsEvents_errMsgEventHandler(this, &frmTrader::Tws1_errMsg);
			// 
			// btnBuy
			// 
			this->btnBuy->BackColor = System::Drawing::SystemColors::Control;
			this->btnBuy->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->btnBuy->ForeColor = System::Drawing::Color::Green;
			this->btnBuy->Location = System::Drawing::Point(6, 80);
			this->btnBuy->Name = L"btnBuy";
			this->btnBuy->Size = System::Drawing::Size(47, 24);
			this->btnBuy->TabIndex = 4;
			this->btnBuy->Text = L"Buy";
			this->btnBuy->UseVisualStyleBackColor = false;
			this->btnBuy->Click += gcnew System::EventHandler(this, &frmTrader::btnBuy_Click);
			// 
			// txtStop
			// 
			this->txtStop->BackColor = System::Drawing::Color::White;
			this->txtStop->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->txtStop->Location = System::Drawing::Point(115, 21);
			this->txtStop->Name = L"txtStop";
			this->txtStop->Size = System::Drawing::Size(37, 20);
			this->txtStop->TabIndex = 2;
			this->txtStop->TextChanged += gcnew System::EventHandler(this, &frmTrader::txtStop_TextChanged);
			this->txtStop->GotFocus += gcnew System::EventHandler(this, &frmTrader::txtStop_GotFocus);
			this->txtStop->KeyUp += gcnew System::Windows::Forms::KeyEventHandler(this, &frmTrader::txtStop_KeyUp);
			// 
			// btnSell
			// 
			this->btnSell->BackColor = System::Drawing::SystemColors::Control;
			this->btnSell->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->btnSell->ForeColor = System::Drawing::Color::Red;
			this->btnSell->Location = System::Drawing::Point(59, 80);
			this->btnSell->Name = L"btnSell";
			this->btnSell->Size = System::Drawing::Size(47, 24);
			this->btnSell->TabIndex = 5;
			this->btnSell->Text = L"Sell";
			this->btnSell->UseVisualStyleBackColor = false;
			this->btnSell->Click += gcnew System::EventHandler(this, &frmTrader::btnBuy_Click);
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->BackColor = System::Drawing::SystemColors::Control;
			this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->label3->Location = System::Drawing::Point(115, 5);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(37, 13);
			this->label3->TabIndex = 0;
			this->label3->Text = L"Stop:";
			// 
			// txtTrigger
			// 
			this->txtTrigger->BackColor = System::Drawing::Color::White;
			this->txtTrigger->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->txtTrigger->Location = System::Drawing::Point(58, 21);
			this->txtTrigger->Name = L"txtTrigger";
			this->txtTrigger->Size = System::Drawing::Size(51, 20);
			this->txtTrigger->TabIndex = 1;
			this->txtTrigger->KeyUp += gcnew System::Windows::Forms::KeyEventHandler(this, &frmTrader::txtTrigger_KeyUp);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->BackColor = System::Drawing::SystemColors::Control;
			this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->label2->Location = System::Drawing::Point(58, 5);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(51, 13);
			this->label2->TabIndex = 0;
			this->label2->Text = L"Trigger:";
			// 
			// txtSymbol
			// 
			this->txtSymbol->BackColor = System::Drawing::Color::White;
			this->txtSymbol->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->txtSymbol->Location = System::Drawing::Point(5, 21);
			this->txtSymbol->Name = L"txtSymbol";
			this->txtSymbol->Size = System::Drawing::Size(44, 20);
			this->txtSymbol->TabIndex = 0;
			this->txtSymbol->TextChanged += gcnew System::EventHandler(this, &frmTrader::txtSymbol_TextChanged);
			this->txtSymbol->GotFocus += gcnew System::EventHandler(this, &frmTrader::txtSymbol_GotFocus);
			this->txtSymbol->LostFocus += gcnew System::EventHandler(this, &frmTrader::txtSymbol_LostFocus);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->BackColor = System::Drawing::SystemColors::Control;
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->label1->Location = System::Drawing::Point(5, 5);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(51, 13);
			this->label1->TabIndex = 0;
			this->label1->Text = L"Symbol:";
			// 
			// lstErr
			// 
			this->lstErr->FormattingEnabled = true;
			this->lstErr->Location = System::Drawing::Point(0, 0);
			this->lstErr->Name = L"lstErr";
			this->lstErr->Size = System::Drawing::Size(612, 160);
			this->lstErr->TabIndex = 6;
			this->lstErr->TabStop = false;
			// 
			// btnOptions
			// 
			this->btnOptions->BackColor = System::Drawing::SystemColors::Control;
			this->btnOptions->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->btnOptions->Location = System::Drawing::Point(225, 80);
			this->btnOptions->Name = L"btnOptions";
			this->btnOptions->Size = System::Drawing::Size(59, 24);
			this->btnOptions->TabIndex = 8;
			this->btnOptions->TabStop = false;
			this->btnOptions->Text = L"Options";
			this->btnOptions->UseVisualStyleBackColor = false;
			this->btnOptions->Click += gcnew System::EventHandler(this, &frmTrader::btnOptions_Click);
			// 
			// label10
			// 
			this->label10->AutoSize = true;
			this->label10->BackColor = System::Drawing::SystemColors::Control;
			this->label10->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->label10->Location = System::Drawing::Point(161, 5);
			this->label10->Name = L"label10";
			this->label10->Size = System::Drawing::Size(35, 13);
			this->label10->TabIndex = 0;
			this->label10->Text = L"Size:";
			// 
			// txtSize
			// 
			this->txtSize->BackColor = System::Drawing::Color::White;
			this->txtSize->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->txtSize->Location = System::Drawing::Point(161, 21);
			this->txtSize->Name = L"txtSize";
			this->txtSize->Size = System::Drawing::Size(59, 20);
			this->txtSize->TabIndex = 3;
			this->txtSize->TabStop = false;
			this->txtSize->KeyUp += gcnew System::Windows::Forms::KeyEventHandler(this, &frmTrader::txtSize_KeyUp);
			// 
			// btnLimit
			// 
			this->btnLimit->BackColor = System::Drawing::SystemColors::Control;
			this->btnLimit->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->btnLimit->Location = System::Drawing::Point(226, 17);
			this->btnLimit->Name = L"btnLimit";
			this->btnLimit->Size = System::Drawing::Size(47, 24);
			this->btnLimit->TabIndex = 8;
			this->btnLimit->TabStop = false;
			this->btnLimit->Text = L"Limits";
			this->btnLimit->UseVisualStyleBackColor = false;
			this->btnLimit->Click += gcnew System::EventHandler(this, &frmTrader::btnLimit_Click);
			// 
			// btnCloseAll
			// 
			this->btnCloseAll->BackColor = System::Drawing::SystemColors::Control;
			this->btnCloseAll->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->btnCloseAll->ForeColor = System::Drawing::Color::Black;
			this->btnCloseAll->Location = System::Drawing::Point(112, 80);
			this->btnCloseAll->Name = L"btnCloseAll";
			this->btnCloseAll->Size = System::Drawing::Size(59, 24);
			this->btnCloseAll->TabIndex = 5;
			this->btnCloseAll->TabStop = false;
			this->btnCloseAll->Text = L"X";
			this->btnCloseAll->UseVisualStyleBackColor = false;
			this->btnCloseAll->Click += gcnew System::EventHandler(this, &frmTrader::btnCloseAll_Click);
			// 
			// btnClearLog
			// 
			this->btnClearLog->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(192)), 
				static_cast<System::Int32>(static_cast<System::Byte>(192)));
			this->btnClearLog->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->btnClearLog->Location = System::Drawing::Point(67, 168);
			this->btnClearLog->Name = L"btnClearLog";
			this->btnClearLog->Size = System::Drawing::Size(59, 24);
			this->btnClearLog->TabIndex = 8;
			this->btnClearLog->TabStop = false;
			this->btnClearLog->Text = L"Clear";
			this->btnClearLog->UseVisualStyleBackColor = false;
			this->btnClearLog->Click += gcnew System::EventHandler(this, &frmTrader::btnClearLog_Click);
			// 
			// tbStrategies
			// 
			this->tbStrategies->Controls->Add(this->tb1);
			this->tbStrategies->Controls->Add(this->tb2);
			this->tbStrategies->Location = System::Drawing::Point(0, 0);
			this->tbStrategies->Name = L"tbStrategies";
			this->tbStrategies->SelectedIndex = 0;
			this->tbStrategies->Size = System::Drawing::Size(288, 74);
			this->tbStrategies->TabIndex = 14;
			this->tbStrategies->TabStop = false;
			this->tbStrategies->SelectedIndexChanged += gcnew System::EventHandler(this, &frmTrader::tbStrategies_SelectedIndexChanged);
			// 
			// tb1
			// 
			this->tb1->Controls->Add(this->label1);
			this->tb1->Controls->Add(this->txtSymbol);
			this->tb1->Controls->Add(this->btnLimit);
			this->tb1->Controls->Add(this->label2);
			this->tb1->Controls->Add(this->txtTrigger);
			this->tb1->Controls->Add(this->label3);
			this->tb1->Controls->Add(this->txtSize);
			this->tb1->Controls->Add(this->label10);
			this->tb1->Controls->Add(this->txtStop);
			this->tb1->Location = System::Drawing::Point(4, 22);
			this->tb1->Name = L"tb1";
			this->tb1->Padding = System::Windows::Forms::Padding(3);
			this->tb1->Size = System::Drawing::Size(280, 48);
			this->tb1->TabIndex = 1;
			this->tb1->Text = L"Strategy Options";
			this->tb1->UseVisualStyleBackColor = true;
			// 
			// tb2
			// 
			this->tb2->Controls->Add(this->btnCopy);
			this->tb2->Controls->Add(this->label5);
			this->tb2->Controls->Add(this->btnClearLog);
			this->tb2->Controls->Add(this->lstActiveTriggers);
			this->tb2->Controls->Add(this->lstErr);
			this->tb2->Location = System::Drawing::Point(4, 22);
			this->tb2->Name = L"tb2";
			this->tb2->Padding = System::Windows::Forms::Padding(3);
			this->tb2->Size = System::Drawing::Size(280, 48);
			this->tb2->TabIndex = 2;
			this->tb2->Text = L"Log";
			this->tb2->UseVisualStyleBackColor = true;
			// 
			// btnCopy
			// 
			this->btnCopy->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(128)), 
				static_cast<System::Int32>(static_cast<System::Byte>(128)));
			this->btnCopy->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->btnCopy->Location = System::Drawing::Point(2, 168);
			this->btnCopy->Name = L"btnCopy";
			this->btnCopy->Size = System::Drawing::Size(59, 24);
			this->btnCopy->TabIndex = 8;
			this->btnCopy->TabStop = false;
			this->btnCopy->Text = L"Copy";
			this->btnCopy->UseVisualStyleBackColor = false;
			this->btnCopy->Click += gcnew System::EventHandler(this, &frmTrader::btnCopy_Click);
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->BackColor = System::Drawing::SystemColors::Control;
			this->label5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->label5->Location = System::Drawing::Point(618, 3);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(57, 26);
			this->label5->TabIndex = 0;
			this->label5->Text = L"Active\nTriggers:";
			this->label5->TextAlign = System::Drawing::ContentAlignment::TopCenter;
			// 
			// lstActiveTriggers
			// 
			this->lstActiveTriggers->FormattingEnabled = true;
			this->lstActiveTriggers->Location = System::Drawing::Point(618, 32);
			this->lstActiveTriggers->Name = L"lstActiveTriggers";
			this->lstActiveTriggers->Size = System::Drawing::Size(64, 160);
			this->lstActiveTriggers->TabIndex = 15;
			this->lstActiveTriggers->TabStop = false;
			this->lstActiveTriggers->SelectedIndexChanged += gcnew System::EventHandler(this, &frmTrader::lstActiveTriggers_SelectedIndexChanged);
			// 
			// ckOnTop
			// 
			this->ckOnTop->AutoSize = true;
			this->ckOnTop->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(177)));
			this->ckOnTop->Location = System::Drawing::Point(216, 3);
			this->ckOnTop->Name = L"ckOnTop";
			this->ckOnTop->Size = System::Drawing::Size(75, 17);
			this->ckOnTop->TabIndex = 13;
			this->ckOnTop->TabStop = false;
			this->ckOnTop->Text = L"On Top\?";
			this->ckOnTop->UseVisualStyleBackColor = true;
			this->ckOnTop->CheckedChanged += gcnew System::EventHandler(this, &frmTrader::ckOnTop_CheckedChanged);
			// 
			// frmTrader
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(292, 110);
			this->Controls->Add(this->Tws1);
			this->Controls->Add(this->ckOnTop);
			this->Controls->Add(this->tbStrategies);
			this->Controls->Add(this->btnOptions);
			this->Controls->Add(this->btnCloseAll);
			this->Controls->Add(this->btnSell);
			this->Controls->Add(this->btnBuy);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedToolWindow;
			this->Name = L"frmTrader";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"DA BG Trader";
			this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &frmTrader::frmTrader_FormClosing);
			this->Load += gcnew System::EventHandler(this, &frmTrader::frmTrader_Load);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->Tws1))->EndInit();
			this->tbStrategies->ResumeLayout(false);
			this->tb1->ResumeLayout(false);
			this->tb1->PerformLayout();
			this->tb2->ResumeLayout(false);
			this->tb2->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

	public:
		Account ^acc;
		array<Bars^> ^triggers;
		int next_trigger;

	private: System::Void frmTrader_Load(System::Object^  sender, System::EventArgs^  e)
			 {
				 this->Height = 131;
				 this->Width = 296;
				 acc = gcnew Account(Tws1);
				 next_trigger = 0;
				 triggers = gcnew array<Bars^>(100);

				 StreamReader^ sr;
				 try
				 {
					 sr = gcnew StreamReader("Data.txt");
					 acc->LoadDataFromFile(sr);
				 }
				 catch (Exception ^e)
				 {
					 if (sr != nullptr) sr->Close();
					 acc = gcnew Account(Tws1);
				 }
				 
				 this->Show();
				 this->Focus();
				 txtSymbol->Focus();
			 }

	private: System::Void frmTrader_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e)
			 {
				 MessageBoxButtons buttons = MessageBoxButtons::YesNo;
				 System::Windows::Forms::DialogResult result;

				 result = MessageBox::Show(this, "Are you sure you want to quit?", "Quit Program", buttons);
				 if (result == ::DialogResult::No) e->Cancel = true;

				 acc->SaveDataToFile();
			 }

	private: void DisableAllControls()
			 {
				 txtSymbol->Enabled = false;
				 txtTrigger->Enabled = false;
				 txtStop->Enabled = false;
				 txtSize->Enabled = false;
				 btnLimit->Enabled = false;
				 btnBuy->Enabled = false;
				 btnSell->Enabled = false;
				 btnOptions->Enabled = false;
				 btnCloseAll->Enabled = false;
			 }

	private: void EnableAllControls()
			 {
				 txtSymbol->Enabled = true;
				 txtTrigger->Enabled = true;
				 txtStop->Enabled = true;
				 txtSize->Enabled = true;
				 btnLimit->Enabled = true;
				 btnBuy->Enabled = true;
				 btnSell->Enabled = true;
				 btnOptions->Enabled = true;
				 btnCloseAll->Enabled = true;
			 }

	private: System::Void txtSymbol_TextChanged(System::Object^  sender, System::EventArgs^  e)
			 {
				 txtSymbol->Text = txtSymbol->Text->ToUpper();
				 txtSymbol->SelectionStart = 100;
			 }

	private: System::Void txtSymbol_LostFocus(System::Object^  sender, System::EventArgs^  e)
			 {
			 	 if (txtSymbol->Text == "") return;

		 		 TWSLib::IContract ^ic = Tws1->createContract();

				 txtSymbol->Text = txtSymbol->Text->ToUpper();
				 ic->symbol = txtSymbol->Text;
				 ic->exchange = "SMART";
				 ic->currency = "USD";
				 ic->secType = "STK";
				 Tws1->cancelMktData(999999);
				 Tws1->reqMktDataEx(999999, ic, "", 1);
			 }

	private: System::Void txtSymbol_GotFocus(System::Object^  sender, System::EventArgs^  e)
			 {
				 txtSymbol->Text = "";
				 txtTrigger->Text = "";
				 txtStop->Text = "";
				 txtSize->Text = "";
			 }

	private: System::Void Tws1_tickPrice(System::Object^  sender, AxTWSLib::_DTwsEvents_tickPriceEvent^  e)
			 {
				 if (e->tickType == 4 && e->id == 999999)
					 txtTrigger->Text = Convert::ToString(e->price);
			 }

	private: System::Void txtTrigger_KeyUp(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)
			 {
				 if (!Information::IsNumeric(txtTrigger->Text)) return;

				 if (e->KeyCode == Keys::Up && e->Shift == false)
					txtTrigger->Text = Convert::ToString(Convert::ToDouble(txtTrigger->Text) + 0.01);

				 if (e->KeyCode == Keys::Down && e->Shift == false)
					txtTrigger->Text = Convert::ToString(Convert::ToDouble(txtTrigger->Text) - 0.01);

				 if (e->KeyCode == Keys::Up && e->Shift == true)
					 txtTrigger->Text = Convert::ToString(Convert::ToDouble(txtTrigger->Text) + 0.05);

				 if (e->KeyCode == Keys::Down && e->Shift == true)
					 txtTrigger->Text = Convert::ToString(Convert::ToDouble(txtTrigger->Text) - 0.05);
				 
				 if (Convert::ToDouble(txtTrigger->Text) < 0) txtTrigger->Text = "0";

				 txtTrigger->SelectionStart = 10;
			 }

	private: System::Void txtStop_GotFocus(System::Object^  sender, System::EventArgs^  e)
			 {
				 if (txtStop->Text != "") return;

				 txtStop->Text = "0.";
				 txtStop->SelectionStart = 10;
			 }

	private: System::Void txtStop_TextChanged(System::Object^  sender, System::EventArgs^  e)
			 {
				 if (!Information::IsNumeric(txtTrigger->Text) || !Information::IsNumeric(txtStop->Text) ) return;

				 float triggerSpread, stopLossPrice, triggerStopPrice, triggerLimitPrice, riskByCents;
				 int size;

				 triggerSpread = (int)(Convert::ToDouble(txtStop->Text) * 10);
				 triggerSpread /= 100;
				 if (triggerSpread == 0) triggerSpread = 0.01;

				 stopLossPrice = Convert::ToDouble(txtTrigger->Text) - Convert::ToDouble(txtStop->Text);
				 triggerStopPrice = Convert::ToDouble(txtTrigger->Text) + 0.01;
				 triggerLimitPrice = triggerStopPrice + triggerSpread;
				 riskByCents = triggerLimitPrice - stopLossPrice;
				 size = acc->RiskInDollar / riskByCents;
				 size = (size / 10) * 10;

				 txtSize->Text = Convert::ToString(size);
				 txtSize->Tag = txtSize->Text;
				 txtSize->BackColor = Color::White;
				 label10->Text = "Size (max " + Convert::ToString(txtSize->Tag) + "):";
			 }

	private: System::Void txtStop_KeyUp(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)
			 {
				 if (!Information::IsNumeric(txtStop->Text)) return;

				 if ((e->KeyCode == Keys::Up || e->KeyCode == Keys::Down) && txtStop->Text == "0.") txtStop->Text = "0";

				 if (e->KeyCode == Keys::Up && e->Shift == false) txtStop->Text = Convert::ToString(Convert::ToDouble(txtStop->Text) + 0.01);

				 if (e->KeyCode == Keys::Down && e->Shift == false) txtStop->Text = Convert::ToString(Convert::ToDouble(txtStop->Text) - 0.01);

				 if (e->KeyCode == Keys::Up && e->Shift == true) txtStop->Text = Convert::ToString(Convert::ToDouble(txtStop->Text) + 0.05);

				 if (e->KeyCode == Keys::Down && e->Shift == true) txtStop->Text = Convert::ToString(Convert::ToDouble(txtStop->Text) - 0.05);

				 if (Convert::ToDouble(txtStop->Text) < 0) txtStop->Text = "0";

				 txtStop->SelectionStart = 10;
			 }

	private: System::Void txtSize_KeyUp(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)
			 {
				 if (!Information::IsNumeric(txtSize->Text)) return;

				 if (e->KeyCode == Keys::Up && e->Shift == false) txtSize->Text = Convert::ToString(Convert::ToInt64(txtSize->Text) + 10);

				 if (e->KeyCode == Keys::Down && e->Shift == false) txtSize->Text = Convert::ToString(Convert::ToInt64(txtSize->Text) - 10);

				 if (e->KeyCode == Keys::Up && e->Shift == true) txtSize->Text = Convert::ToString(Convert::ToInt64(txtSize->Text) + 50);

				 if (e->KeyCode == Keys::Down && e->Shift == true) txtSize->Text = Convert::ToString(Convert::ToInt64(txtSize->Text) - 50);

				 if (Convert::ToInt64(txtSize->Text) < 0) txtSize->Text = "0";

				 if (Convert::ToInt64(txtSize->Text) > Convert::ToInt64(txtSize->Tag) || Convert::ToInt64(txtSize->Text) < 10)
					 txtSize->BackColor = Color::IndianRed;
				 else
					 txtSize->BackColor = Color::White;

				 txtSize->SelectionStart = 10;
			 }

	private: System::Void Tws1_errMsg(System::Object^  sender, AxTWSLib::_DTwsEvents_errMsgEvent^  e)
			 {
				 if (e->errorMsg->Contains("999999")) return;
				 if (e->errorMsg->Contains("Order Canceled")) return;
				 if (e->errorMsg->Contains("Can't find order")) return;
				 if (e->errorMsg->Contains("Market data farm")) return;
				 if (e->errorMsg->Contains("HMDS data farm")) return;
				 if (e->errorMsg->Contains("Historical Market Data Service query")) return;

				 lstErr->Items->Insert(0, GlobalTime::GetTime() + "IB Message: " + e->errorMsg);
			 }

	private: System::Void btnBuy_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 if (next_trigger == 100)
					{ MessageBox::Show("Not support more than 100 triggers right now, sorry"); return; }
				 if (txtSymbol->Text == "" || !Information::IsNumeric(txtTrigger->Text) || !Information::IsNumeric(txtStop->Text) || txtStop->Text == "0." || Convert::ToDouble(txtStop->Text) == 0 || !Information::IsNumeric(txtSize->Text) || Convert::ToInt64(txtSize->Text) <= 0 || txtSize->BackColor != Color::White)
					{ MessageBox::Show("Please fill all fields correctly!"); return; }

				 String ^str = gcnew String(Convert::ToString(DateTime::Today));
				 Button ^btn = dynamic_cast<Button^>(sender);
				 Side sd;

				 if (btn->Text == "Buy") sd = sLONG;
				 if (btn->Text == "Sell") sd = sSHORT;

				 triggers[next_trigger] = gcnew Bars(txtSymbol->Text, acc);
				 triggers[next_trigger]->ReqHistory(str->Substring(6, 4) + str->Substring(3, 2) + str->Substring(0, 2) + " 23:00:00");
				 triggers[next_trigger]->CreateTrigger(Convert::ToSingle(txtTrigger->Text), Convert::ToSingle(txtStop->Text), Convert::ToInt32(txtSize->Text), sd);
				 next_trigger++;

				 lstErr->Items->Insert(0, GlobalTime::GetTime() + txtSymbol->Text + " - Order Sent, waiting for break at " + txtTrigger->Text + " (stop " + triggers[next_trigger - 1]->Trg->StopLossPrice + ")");
				 lstActiveTriggers->Items->Insert(0, txtSymbol->Text);
				 txtSymbol->Focus();
			 }

	private: System::Void btnOptions_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 if (next_trigger > 0) MessageBox::Show("You performed one trigger, please try not to change any parameters right now\nThis 'option' button going to disable soon after you perform 1 trigger");

				 frmOptions ^frmO = gcnew frmOptions(acc);

				 frmO->ShowDialog(this);
			 }

	private: System::Void btnLimit_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 frmStrategy1 ^frmS = gcnew frmStrategy1(acc);

				 frmS->ShowDialog(this);
			 }

	private: System::Void btnCloseAll_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 if (next_trigger == 0) return;

				 MessageBoxButtons buttons = MessageBoxButtons::YesNo;
				 System::Windows::Forms::DialogResult result;

				 result = MessageBox::Show(this, "Are you sure you want to CLOSE ALL OPEN POSITIONS??", "Close All Open Positions", buttons);
				 if (result == ::DialogResult::No) return;

				 int i;

				 for (i=0; i < next_trigger; i++)
					triggers[i]->Trg->CloseTrigger(true);
			 }

	private: System::Void ckOnTop_CheckedChanged(System::Object^  sender, System::EventArgs^  e)
			 {
				 if (ckOnTop->Checked == true)
					this->TopMost = true;
				 else
					 this->TopMost = false;
			 }

	private: System::Void btnClearLog_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 lstErr->Items->Clear();
			 }

	private: System::Void btnCopy_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 String ^str = gcnew String("");
				 int i;

				 for (i=0; i<lstErr->Items->Count; i++)
					 str += lstErr->Items[i]->ToString() + Environment::NewLine;

				 if (str != "") Clipboard::SetText(str);
			 }

	private: System::Void lstActiveTriggers_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e)
			 {
				 int i;

				 for (i=0; i<next_trigger; i++)
					 if (Convert::ToString(triggers[i]->SymbolName) == Convert::ToString(lstActiveTriggers->SelectedItem))
					 {
						 MessageBox::Show( "Symbol: " + triggers[i]->Trg->SymbolName + "\nTrend: " + (triggers[i]->Trg->Trend == sLONG ? "Long":"Short") + "\nTrigger Price: " + triggers[i]->Trg->TriggerPrice + "\nStopLoss Price: " + triggers[i]->Trg->StopLossPrice + "\nOriginal StopLoss Price: " + triggers[i]->Trg->OriginalStopLossPrice + "\nQuantity Filled: " + triggers[i]->Trg->QuantityFilled + "\nLimit 1 - " + triggers[i]->Trg->LimitP->Profit_1_price + " (" + triggers[i]->Trg->LimitP->Profit_1_size + ")" + ( triggers[i]->Trg->LimitP->Profit_2_price > 0 ? "\nLimit 2 - " + triggers[i]->Trg->LimitP->Profit_2_price + " (" + triggers[i]->Trg->LimitP->Profit_2_size + ")" : "" ) + ( triggers[i]->Trg->LimitP->Profit_3_price > 0 ? "\nLimit 3 - " + triggers[i]->Trg->LimitP->Profit_3_price + " (" + triggers[i]->Trg->LimitP->Profit_3_size + ")" : "" ) + ( triggers[i]->Trg->LimitP->Profit_4_price > 0 ? "\nLimit 4 - " + triggers[i]->Trg->LimitP->Profit_4_price + " (" + triggers[i]->Trg->LimitP->Profit_4_size + ")" : "" ) );
						 break;
					 }
				
				 lstActiveTriggers->SelectedIndex = -1;
			 }


	private: System::Void tbStrategies_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e)
			 {
				 if (tbStrategies->SelectedIndex == 0)
				 {
					 tbStrategies->Size = System::Drawing::Size(288, 74);
					 this->Size = System::Drawing::Size(296, 131);
				 }
				 else
				 {
					 tbStrategies->Size = System::Drawing::Size(694, 223);
					 this->Size = System::Drawing::Size(707, 250);
				 }
			 }
};
}
